import configurate
import error


class ReflectRun(object):

    def __init__(self):
        pass

    def run(self, url, params):
        path, func = url[1:].split("/")
        modules, clazz = path.split(".")
        for app in configurate.apps:
            if (modules.find(app) > -1 and len(modules) == len(app)):
                # 循环配置注册过的model层 循环去匹配路径
                obj = __import__(path, fromlist=True)  # 注意fromlist参数
                if hasattr(obj, func):
                    func = getattr(obj, func)
                    if params:
                        return func(params)
                    else:
                        return func()
                break
        raise error.MyError(error.ErrorCode().METHOD_404)
